<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Models as Models;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }

    //视频播放页面
    public function player(Request $request)
    {
        //视频id
        $vid = isset($request->videoId)?intval($request->videoId):1;
        //视频详情
        $videoObj = new Models\Videos();
        //所有视频
        $video_detail = $videoObj->getVideosById($vid);
        $view_array = array(
            'video_id' => $video_detail->video_id,
            'face' => $video_detail->video_face,
            'address' => $video_detail->video_address
        );
        return view('player',$view_array);
    }

    //列表首页
    public function welcome()
    {
        $videoObj = new Models\Videos();
        //所有视频
        $video_array = $videoObj->getAllVideos();
        $view_array = array(
            'videos' => $video_array
        );
        return view('welcome',$view_array);
    }

    //上传总页面
    public function uploadView()
    {
        return view('upload');
    }

    //上传页面
    public function uploadVideo(Request $request)
    {
        $file = $_FILES['files'];
        //ajax返回数组
        $data = array('sta'=>TRUE,'msg'=>'上传成功！');
        $ext = $this->getExt($file['name']);
        $arrExt = array('3gp','rmvb','flv','wmv','avi','mkv','mp4','mp3','wav');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            $data['msg'] = '不支持此类型文件的上传！';
        }
        $previewPath = 'video';
        if($file['error'] == 0) {
            //文件上传到预览目录
            $previewName = 'pre_'.md5(mt_rand(1000,9999)).time().'.'.$ext;
            $previewSrc = $previewPath.$previewName;
            if(!move_uploaded_file($file['tmp_name'],$previewSrc)) {
                $data['sta'] = FALSE;
                $data['msg'] = '上传失败！';
            } else {
                $data['previewSrc'] = $previewSrc;
            }
            echo json_encode($data);
        }
    }
    //获取文件扩展名
    function getExt($filename) {
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        return $ext;
    }

    //表情包列表
    public function smileList()
    {
        $smileObj = new Models\Smiles();
        $smile_array = $smileObj->getSmiles();
        $view_array = array(
            'smiles' => $smile_array
        );
        return view('smileList',$view_array);
    }

    //表情包详情
    public function smileDetail(Request $request)
    {
        $id = $request->id;
        $smileObj = new Models\Smiles();
        $detail = $smileObj->getSmileById($id);
        $view_array = array(
            'id' => $detail->id,
            'url' => $detail->url
        );
        return view('smileDetail',$view_array);
    }
}
